package me.pplay.playtalk.view;

import me.pplay.playtalk.R;
import me.pplay.playtalk.protocol.DataCallBack;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;


public class DialogMenu {
	protected PopupWindow mPopupWindow;
	public static final int DIALOG_1 = 81;
	public static final int DIALOG_2 = 82;
	public static final int DIALOG_3 = 83;
	public static final int DIALOG_4 = 84;
	public static final int DIALOG_5 = 85;
	private Context mContext;
	private int[] menu_drawableIds;
	private String[] menu_titles;
	private AlertDialog dialog;
	private DataCallBack callback;
	private int icon;
	private String title;
	private View mParentView;
	
	
	
	private LinearLayout layout_b, layout_c, layout_1, layout_2, layout_3, layout_4, layout_5;
	private ImageView image_1, image_2, image_3, image_4, image_5, dialog_icon;
	private TextView tv_1, tv_2, tv_3, tv_4, tv_5, dialog_title;

	
	public DialogMenu(Context context, View parentView, int icon, String title, int[] menu_drawableIds, String[] menu_titles, DataCallBack callback){
		this.mContext			= context;
		this.icon				= icon;
		this.title				= title;
		this.menu_drawableIds	= menu_drawableIds;
		this.menu_titles		= menu_titles;
		this.mParentView 		= parentView;
		this.callback			= callback;
		dialog();
	}
	
	private void dialog(){
		View menuview 	= LayoutInflater.from(mContext).inflate(R.layout.layout_menu, null);
		layout_b    	= (LinearLayout)menuview.findViewById(R.id.layout_b);
		layout_c   	 	= (LinearLayout)menuview.findViewById(R.id.layout_c);
		layout_1    	= (LinearLayout)menuview.findViewById(R.id.layout_1);
		layout_2    	= (LinearLayout)menuview.findViewById(R.id.layout_2);
		layout_3    	= (LinearLayout)menuview.findViewById(R.id.layout_3);
		layout_4    	= (LinearLayout)menuview.findViewById(R.id.layout_4);
		layout_5    	= (LinearLayout)menuview.findViewById(R.id.layout_5);
		image_1			= (ImageView)menuview.findViewById(R.id.image_1);
		image_2			= (ImageView)menuview.findViewById(R.id.image_2);
		image_3			= (ImageView)menuview.findViewById(R.id.image_3);
		image_4			= (ImageView)menuview.findViewById(R.id.image_4);
		image_5			= (ImageView)menuview.findViewById(R.id.image_5);
		dialog_icon		= (ImageView)menuview.findViewById(R.id.dialog_icon);
		tv_1			= (TextView)menuview.findViewById(R.id.tv_1);
		tv_2			= (TextView)menuview.findViewById(R.id.tv_2);
		tv_3			= (TextView)menuview.findViewById(R.id.tv_3);
		tv_4			= (TextView)menuview.findViewById(R.id.tv_4);
		tv_5			= (TextView)menuview.findViewById(R.id.tv_5);
		dialog_title	= (TextView)menuview.findViewById(R.id.dialog_title);
		
		
		dialog_icon.setBackgroundResource(icon);
		dialog_title.setText(title);
		if(menu_titles.length == 2){
			layout_b.setVisibility(View.GONE);
			layout_c.setVisibility(View.GONE);
		}else if(menu_titles.length == 4){
			layout_c.setVisibility(View.GONE);
		}
		
		
		
		for (int i = 0; i < menu_titles.length; i++) {
			switch (i) {
			case 0:
				image_1.setBackgroundResource(menu_drawableIds[i]);
				tv_1.setText(menu_titles[i]);
				layout_1.setOnClickListener(new ClickListener(0));
				break;
			case 1:
				image_2.setBackgroundResource(menu_drawableIds[i]);
				tv_2.setText(menu_titles[i]);
				layout_2.setOnClickListener(new ClickListener(1));
				break;
			case 2:
				image_3.setBackgroundResource(menu_drawableIds[i]);
				tv_3.setText(menu_titles[i]);
				layout_3.setOnClickListener(new ClickListener(2));
				break;
			case 3:
				image_4.setBackgroundResource(menu_drawableIds[i]);
				tv_4.setText(menu_titles[i]);
				layout_4.setOnClickListener(new ClickListener(3));
				break;
			case 4:
				image_5.setBackgroundResource(menu_drawableIds[i]);
				tv_5.setText(menu_titles[i]);
				layout_5.setOnClickListener(new ClickListener(4));
				break;
			}
		}
		mPopupWindow = new PopupWindow(menuview, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    	mPopupWindow.setFocusable(true);
    	ColorDrawable dw = new ColorDrawable(0x00);
		mPopupWindow.setBackgroundDrawable(dw);
	}
	
	public void show(boolean bShow)
	{

		if (bShow)
		{		
			mPopupWindow.showAtLocation(mParentView, Gravity.TOP, 0, 0);
		}else{
			mPopupWindow.dismiss();
		}
	}
	
	class ClickListener implements OnClickListener{
		private int position = 0;
		public ClickListener(int index){
			position = index;
		}

		@Override
		public void onClick(View v) {
			callback.Callback(position, null, null);
		}
	}
	
/*	public void showMenu(){
//		dialog.show();
	}
	
	public boolean isShowing(){
		return false;
//		return dialog.isShowing();
	}
	
	public void cancelMenu(){
//		dialog.dismiss();
	}*/
}
